package com.mm.learn.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author wdx
 * @create 2021-09-23 11:42
 * 策略上下文
 */
@Component
public class LoginContext {

    @Autowired
    Set<AbstractLogin> serviceSet;

    Map<Integer, AbstractLogin> map;

    public String login(String username,String password,Integer loginType){
        AbstractLogin abstractLogin = map.get(loginType);
        if (abstractLogin == null){
            return "这个登陆方式不存在";
        }
        return abstractLogin.login(username,password);
    }

    @PostConstruct
    public void init(){
        map = new HashMap<>();
        for (AbstractLogin abstractLogin : serviceSet) {
            map.put(abstractLogin.id(),abstractLogin);
        }

    }



}
